<?php
/**
 * 《物料需求》实现类
 *
 * @author 孙晓晔
 * @version $Id: Material.class.php 1059 2010-04-08 07:55:21Z sunxy $
 */
require_once(dirname(__FILE__) . '/Material.class.php');

class Bom extends Material {
	public $tbl = Constant::tbl_material_bom;

	public $type = array(
		'-' => '请选择',
		'1' => '标准件',
		'2' => '长方体',
		'3' => '圆柱体',
		'9' => '自定义',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待采购',
		'3' => '正在采购',
		'4' => '已经入库',
		'8' => '使用库存',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'standard' => '规格',
		
		'category' => '大类',
		'subclass' => '小类',		

		'price' => '单价',
		'amount' => '数量',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '申请人',
		'mold' => '产品',
		
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'物料需求' => array('cc'=>'material.php?do=Bom.find', 'js'=>'1'),	
			'采购单' => array('cc'=>'material.php?do=Purchase.find', 'js'=>'1'),
			'采购明细' => array('cc'=>'material.php?do=RecordIn.find', 'js'=>''),
			
			'支出信息' => array('cc'=>'finance.php?do=OutMy.find', 'js'=>''),
		
			'进货单' => array('cc'=>'material.php?do=Receive.find', 'js'=>''),
			'入库明细' => array('cc'=>'material.php?do=In.find', 'js'=>''),
		
			'物料库存' => array('cc'=>'material.php?do=Amount.find', 'js'=>''),
			'出库明细' => array('cc'=>'material.php?do=Out.find', 'js'=>''),
		
			'材料信息' => array('cc'=>'material.php?do=Material.find', 'js'=>''),
			'产品信息' => array('cc'=>'mold.php?do=Mold.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'material.php?do=Bom.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'物料需求' => array('cc'=>'material.php?do=Bom.group', 'js'=>''),
			'采购单' => array('cc'=>'material.php?do=Purchase.group', 'js'=>''),
			'采购明细' => array('cc'=>'material.php?do=RecordIn.group', 'js'=>''),
			'进货单' => array('cc'=>'material.php?do=Receive.group', 'js'=>''),
			'入库明细' => array('cc'=>'material.php?do=In.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 month'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '申请人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'material/bom/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'category') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['category'];
		}

		if ($px == 'subclass') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['subclass'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";
		
		$dd = sprintf(" AND %s = '%s'", "DATE_FORMAT(a.createtime, '%Y-%m')", date('Y-m', strtotime($request['t1_s'])));

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'material/bom/list');
		return true;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		if ($this->_audit('61') || $this->_audit('67')) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" %s", $dept);
		}
	
		return sprintf(" (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {
		$material_s = $this->_input($request['material_s']);

		$name_s = trim(addslashes($request['name_s']));

		$category_s = $request['category_s'];
		$subclass_s = $request['subclass_s'];

		$type_s = $request['type_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($material_s)) {
			$where .= " AND material = '" . $material_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR standard LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($subclass_s) && $subclass_s <> '-') {
			$where .= " AND subclass = '" . $subclass_s . "'";
		}

		if (!empty($type_s) && $type_s <> "-") {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND date <= '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);				

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'material/bom/list');
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _standard_t($type, $standard) {
		if ($type == 2) {
			return $standard;
		}

		if ($type == 3) {
			return $standard;
		}

		return $standard;
	}

	function _standard($material, $type, $default) {
		if ($type == '1') {
			$m = $this->id(Constant::tbl_material, $material);
			return $m['standard'];
		}

		return $default;
	}

	function _unit($material, $type, $default) {
		if ($type == '1') {
			$m = $this->id(Constant::tbl_material, $material);
			return $m['unit'];
		}

		return $default;
	}

	function _price($material, $type, $standard) {
		$m = $this->id(Constant::tbl_material, $material);

		if ($type == 2) {
			$v = $this->_v1($standard);

			return $v * $m['density'] * $m['price'];
		}

		if ($type == 3) {
			$v = $this->_v2($standard);

			return $v * $m['density'] * $m['price'];
		}

		return $m['price'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', strtotime('2 day'));

		$controller->display($request, 'material/bom/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$date = $request['date'];
		$mold = $this->_input($request['mold']);
		$material = $this->_input($request['material']);

		$type = $request['type'];
		$amount = $request['amount'];		

		$name = addslashes($request['name']);
		$standard = addslashes($request['standard']);
		$price2 = $request['price2'];
		$unit2 = $request['unit2'];
		
		$density = $request['density'];
		
		// $m = $this->id(Constant::tbl_material, $material);
		if ($type == 1) {
			// $standard = $m['standard'];
			
			$unit = $unit2;
			$price = $price2;
			$amount2 = $amount;			
		} elseif ($type == 2) {
			$v = $this->_v1($standard);
			if (!empty($density) && $density > 0) {
				$n = $v * $density;
			} else {
				$n = $v;
			}
			
			$unit = $request['unit'];
			$price = $n * $price2;
			
			$amount2 = $n * $amount;
		} elseif ($type == 3) {
			$v = $this->_v2($standard);
			if (!empty($density) && $density > 0) {
				$n = $v * $density;		
			} else {
				$n = $v;
			}	

			$unit = $request['unit'];
			$price = $n * $price2;
			
			$amount2 = $n * $amount;
		} elseif ($type == 9) {
			$unit = $request['unit'];
			$price = $price2;
			$amount = $request['amount'];
			$amount2 = $amount;
		} else {
			$unit = $unit2;
			$price = $price2;
			$amount2 = $amount;	
		}
		
		$used = addslashes($request['used']);
		$content = addslashes($request['content']);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, mold, date, material, type, name, standard, amount, unit, price, amount2, unit2, price2, used, content, emp, oper";
		$fmt .= ", createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $mold, $date, $material, $type, $name, $standard, $amount, $unit, $price, $amount2, $unit2, $price2, $used, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row = $this->_v($row);

		$controller->assign_by_ref('result', $row);

		$controller->assign('t1', strtotime('2 day'));
		
		$controller->display($request, 'material/bom/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$date = $request['date'];
		$mold = $this->_input($request['mold']);
		$material = $this->_input($request['material']);

		$type = $request['type'];

		$name = $request['name'];

		$standard = $this->_standard($material, $type, $request['standard']);
		$unit = $this->_unit($material, $type, $request['unit']);

		$price = $request['price'];
		if (empty($price)) {
			$price = $this->_price($material, $type, $standard);
		}

		$amount = $request['amount'];

		$used = addslashes($request['used']);
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', mold = '%s', date = '%s', material = '%s', type = '%s', name = '%s', standard = '%s', ";
		$fmt .= "unit = '%s', price = '%s', amount = '%s', used = '%s', content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $mold, $date, $material, $type, $name, $standard, $unit, $price, $amount, $used, $content, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return true;
		}
			
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row = $this->_v($row);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'material/bom/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		if (!empty($request['small'])) {			
			require_once(dirname(__FILE__) . '/RecordIn.class.php');
			$in = new RecordIn();
			$record = $in->view_bom($id);
			
			$controller->assign_by_ref('record', $record);
		
			$controller->display($request, 'material/bom/view_small');
			return true;
		}

		require_once 'Material.class.php';
		$material = new Material();
		$controller->assign_by_ref('material', $material->_view($row['material']));

		$controller->display($request, 'material/bom/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['kc'] = $this->_kc($row);

		$row['material_t'] = Name::material($row['material']);
		$row['mold_t'] = Name::mold($row['mold']);
		
		$row['type_t'] = $this->type[$row['type']];
		$row['amount'] = floatval($row['amount']);
		$row['file'] = $this->_pic1($id);

		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'material/bom/verify');
		return true;
	}
	
	function _kc($row) {
		$name = addslashes($row['name']);
		$standard = addslashes($row['standard']);
		$unit = $row['unit'];
		
		require_once(dirname(__FILE__) . '/Amount.class.php');
		$aa = new Amount();
		
		return $aa->_kc($name, $standard, $unit);
	}

	/**
	 * 直接采购
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function purchase($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign('t1', strtotime('3 day'));
		
		$controller->assign_by_ref('tecmold_option', $this->tecmold());

		$controller->display($request, 'material/bom/purchase');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_verify($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
 		}
		
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'material/bom/batch_verify');
		return true;
	}
	
	function _batch_insql($a) {
		// $a = json_decode($cc);		
		if (!$a || $a == NULL) {
			debug("cc is NULL");
			return '';
		}
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($a)) {
			$insql .= "', '" . $v;
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function batch_verifyPost($controller, $request) {
		$this->pretreat($controller);
		
		$insql = $this->_batch_insql($request['cc']);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);

		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $insql);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_record($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['kc'] = $this->_kc($row);
			
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		
		require_once(dirname(__FILE__) . '/Purchase.class.php');
		$aa = new Purchase();
		$purchase_option = $aa->_list();
		
		$controller->assign_by_ref('purchase_option', $purchase_option);

		$controller->display($request, 'material/bom/batch_record');
		return true;
	}
	
	function batch_recordPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$bom = $this->id($this->tbl, $id);
		
		$purchase = $this->_input($request['purchase']);
		$content = sprintf("%s\n%s", addslashes($bom['content']), addslashes($request['content']));
		
		$cc = $request['cc'];
		while (list($k, $v) = each($cc)) {
			$this->_batch_record($v, $purchase, $content);
		}		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_enquiry($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$controller->assign_by_ref('result', $db->fetch_all($result));

		$controller->display($request, 'material/bom/batch_enquiry');
		return true;
	}
	
	function mailPost($controller, $request) {
		$this->pretreat($controller);

		$from = $request['from'];
		$to = $request['to'];

		$subject = $request['subject'];
		$content = $request['content'];
		
		$this->mail_html($from, $to, $subject, $content, $from);

		return true;
	}
	
	function _batch_record($id, $purchase, $content) {
		require_once(dirname(__FILE__) . '/RecordIn.class.php');
		$aa = new RecordIn();
		
		if ($aa->_insert_bom($id, $purchase, $content)) {
			$this->_status($id, "3");
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function enquiry($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row = $this->_v($row);

		$controller->assign_by_ref('result', $row);

		$density = array(
			'-' => '请选择',
			'7.85' => '钢料',
			'8.90' => '铜',			
		);
		$controller->assign_by_ref('density_option', $density);

		$controller->display($request, 'material/bom/enquiry');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function record($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Purchase.class.php');
		$aa = new Purchase();
		$data = $aa->_list();
		
		$controller->assign_by_ref('purchase_option', $data);

		$controller->display($request, 'material/bom/record');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function recordPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];		
		$bom = $this->id($this->tbl, $id);

		$purchase = $this->_input($request['purchase']);

		$this->_batch_record($id, $purchase, $content);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'material/bom/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(price*amount) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(price) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function ajax_purchase($controller, $request) {
		$q = $request['q'];
		$data = $this->_ajax_purchase($q);
		echo json_encode($data);
	}

	function _ajax_purchase($q) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT id, code, name, emp FROM %s WHERE status = 1 AND code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_wl_purchase, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$ret[] = $row;
		}

		return $ret;
	}

	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function u($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE type in ('2', '3') AND createtime > '2011-11-26'";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$amount = $row['amount'];
			$type = $row['type'];
			$standard = $row['standard'];		
			$m = $this->id(Constant::tbl_material, $row['material']);
			if ($type == 1) {
				$standard = $m['standard'];
				
				$unit = $m['unit'];
				$price = $m['price'];
				$amount2 = $amount;			
			} elseif ($type == 2) {
				$v = $this->_v1($standard);
				if (!empty($m['density']) && $m['density'] > 0) {
					$n = $v * $m['density'];
				} else {
					$n = $v;
				}
				
				$unit = $row['unit'];
				$price = $n * $m['price'];
				
				$amount2 = $n * $amount;
			} elseif ($type == 3) {
				$v = $this->_v2($standard);
				if (!empty($m['density']) && $m['density'] > 0) {
					$n = $v * $m['density'];		
				} else {
					$n = $v;
				}	
	
				$unit = $row['unit'];
				$price = $n * $m['price'];
				
				$amount2 = $n * $amount;
			} else {			
				$unit = $row['unit'];
				$price = $m['price'];
				$amount2 = $amount;
			}

			$fmt = "UPDATE %s SET price = '%s', amount2 = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $price, $amount2, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '新增物料需求情况';
		
		$this->result_s = 20;
		$this->groupby_s = 'emp';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cal($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'material/bom/cal');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function calPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);

		$date1 = $request['date1'];
		$date2 = $request['date2'];

		$content = addslashes($request['content']);

		$where = $this->_cal_where($emp, $date1, $date2);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s a, %s b SET a.category = b.category, a.subclass = b.subclass WHERE a.material = b.id %s";
		$sql = sprintf($fmt, $this->tbl, Constant::tbl_material, $where);
		debug($sql);
		$db->query($sql);

		$fmt = "UPDATE %s a, %s b SET a.price = b.price WHERE a.id = b.bom %s";
		$sql = sprintf($fmt, $this->tbl, Constant::tbl_wl_record_in, $where);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _cal_where($emp, $date1, $date2) {
		$where = "";
		if (!empty($emp)) {
			$where .= " AND a.emp = '" . $emp . "'";
		}

		if (!empty($date1)) {
			$where .= " AND a.createtime >= '" . $date1 . "'";
		}

		if (!empty($date2)) {
			$where .= " AND a.createtime <= '" . $date2 . "'";
		}

		return $where;
	}

	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'material/bom/_list_c');
		return true;
	}
}
?>